package com.hc.springbootrabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {
    //自定义JSON格式消息转化配置
    //不配置则需要将消息实现Serializable序列化
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    //发布订阅模式基于配置的方式的实现组件配置
    @Bean
    public Exchange fanoutExchange(){
        return ExchangeBuilder.fanoutExchange("fanout_exchange").build();
    }
    @Bean
    public Queue emailQueue(){
        return new Queue("fanout_queue_email");
    }
    @Bean
    public Queue smsQueue(){
        return new Queue("fanout_queue_sms");
    }
    @Bean
    public Binding bindEmail(){
        return BindingBuilder.bind(emailQueue()).to(fanoutExchange()).with("").noargs();
    }
    @Bean
    public Binding bindSms(){
        return BindingBuilder.bind(smsQueue()).to(fanoutExchange()).with("").noargs();
    }

    //简单模式
    public Queue simple(){
        return new Queue("simple_queue");
    }
    //工作队列模式
    public Queue work(){
        return new Queue("work_queue");
    }

    //设置队列中所有消息过期时间
    public DirectExchange ttlExchange(){
        return new DirectExchange("ttl_queue_exchange",true,false);
    }
    public Queue ttlQueue(){
        Map<String,Object> map = new HashMap<>();
        //队列存活时间
        map.put("x-message-ttl",30000);
        //队列最多消息数
        map.put("x-max-length",1000);
        return new Queue("ttl_queue",true,false,false,map);
    }
    public Binding ttlBinding(){
        return BindingBuilder.bind(ttlQueue()).to(ttlExchange()).with("ttl_queue_key");
    }

    //实现死信功能的组件配置
    public DirectExchange deadExchange(){
        return new DirectExchange("dead.letter.exchange",true,false,null);
    }
    public Queue deadQueue(){
        return new Queue("dead.letter.queue",true,false,false,null);
    }
    public Binding deadBinding(){
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with("dead.letter");
    }
    public DirectExchange businessExchange(){
        return new DirectExchange("test.refuse.exchange",true,false,null);
    }
    public Queue businessQueue(){
        Map<String,Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange","dead.letter.exchange");//给当前队列配置死信交换机
        map.put("x-dead-letter-routing-key","dead.letter");//绑定路由键
        return new Queue("test.refuse.queue",true,false,false,map);
    }
    public Binding businessBinding(){
        return BindingBuilder.bind(businessQueue()).to(businessExchange()).with("test.refuse");
    }

    //实现延迟功能的组件配置
    public Queue delayQueue(){
        Map<String,Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange","dead.letter.exchange");
        map.put("x-dead-letter-routing-key","dead.letter");
        map.put("x-message-ttl",30000); //利用过期时间+死信队列实现延迟
        return new Queue("test.delay.queue",true,false,false,map);
    }
    public Binding delayBinding(){
        return BindingBuilder.bind(delayQueue()).to(businessExchange()).with("test.delay");
    }
}
